<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:FilterExecutor.php,2010/05/12 11:36:58 
 * 
 **************************************************************************/
 
 
 
/**
 * @file FilterExecutor.php
 * @author huqingping
 * @date 2010/05/12 11:36:58
 * @version 1.0 
 * @brief 
 *  
 **/

require_once  FR_ROOT.'/IFilter.php';

class FilterExecutor
{
	private $impFilters = array();
	private $app = null;

	function __construct(WebApp $app) 
	{
		$this->app = $app;
	}

	function loadFilters($filters)
	{
		foreach($filters as $key => $classes) {
			foreach($classes as $classname) {
				if (strpos($classname,'.') !== false) {
					//避免引用到其他目录
					throw new Exception('hapn.errpath');
				}
				if (!is_readable(FR_ROOT.'filters/'.$classname.'.php')) {
					//支持扩展的一种方式
					if (!is_readable(PLUGIN_ROOT.'filters/'.$classname.'.php')) {
						throw new Exception('hapn.errclass '.$classname);
					}
					require_once PLUGIN_ROOT.'filters/'.$classname.'.php';
				} else {
					require_once FR_ROOT.'filters/'.$classname.'.php';
				}
				if (!class_exists($classname)) {
					throw new Exception('hapn.errclass');
				}
				//Logger::debug('load filter %s.%s',$key,$classname);
				$this->impFilters[$key][] = new $classname();	
			}
		}
	}

	function executeFilter($filtername)
	{
		if (!isset($this->impFilters[$filtername])) {
			//Logger::debug('miss filter %s',$filtername);
			return true;
		}
		$this->app->timer->begin('f_'.$filtername);
		$filters = $this->impFilters[$filtername];
		foreach($filters as $filter) {
			if ($filter->execute($this->app) === false) {
				Logger::debug('call filter %s.%s=false',$filtername,get_class($filter));
				$this->app->timer->end('f_'.$filtername);
				return false;
			} 
			//Logger::debug('call filter %s.%s=true',$filtername, get_class($filter));
		}
		$this->app->timer->end('f_'.$filtername);
		return true;
	}
}





/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */